const path = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const env = require('./env.' + process.env.NODE_ENV)

function resolve (dir) {
    return path.join(__dirname, './', dir)
}


module.exports = {
    mode: 'none',
    entry: './src/main.js',
    output: {
        filename: '[name]-[hash]:8].js',
        path: path.join(__dirname, 'dist')
    },
    resolve: {
        extensions: ['.js', '.vue', '.json'],
        alias: {
            '@': resolve('src'),
        }
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            {
                test: /\.js$/,
                use: "babel-loader",
                exclude: /node_modules/
            },
            {
                test: /\.(vue|js)$/,
                loader: 'eslint-loader',
                exclude: /node_modules/,
                enforce: 'pre' // 预处理
            },
            {
                test: /\.(css|less)$/,
                use: [
                    'style-loader',
                    'vue-style-loader',
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.(png|jpe?g|gif)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        esModule: false,
                        limit: 10 * 1024 // 10 KB
                    }
                }
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': env
        }),
        // 用于生成 index.html
        new HtmlWebpackPlugin({
            title: 'vue',
            url: './',
            template: './public/index.html'
        }),
    ]
}